using Microsoft.EntityFrameworkCore;
using net_work.Common.Request;
using net_work.Common.Result;
using net_work.Config;
using net_work.Data.DTO.Sys;
using net_work.Data.Entity.Sys;
using net_work.Data.VO.Sys;
using net_work.IRepository.Sys;

namespace net_work.Repository.Sys;

public class TeacherRepository(ApplicationDbContext dbContext) : Repository<Teacher>(dbContext), ITeacherRepository
{
    public async Task<long> InsertAsync(TeacherAddRequest teacherAddRequest)
    {
        var newTeacher = new Teacher
        {
            TcrMajor = teacherAddRequest.TcrMajor,
            TcrQual = teacherAddRequest.TcrQual,
            TcrEduBg = teacherAddRequest.TcrEduBg,
            TcrWorkExp = teacherAddRequest.TcrWorkExp,
            TcrWorkAddr = teacherAddRequest.TcrWorkAddr,
            TcrEmpDate = teacherAddRequest.TcrEmpDate,
            ColleId = teacherAddRequest.ColleId,
            DepId = teacherAddRequest.DepId,
            PsnId = teacherAddRequest.PsnId,
            Remark = teacherAddRequest.Remark
        };
        await dbContext.Teachers.AddAsync(newTeacher);
        await dbContext.SaveChangesAsync();
        return newTeacher.Id;
    }

    public async Task<bool> UpdateAsync(TeacherUpdateRequest teacherUpdateRequest)
    {
        var teacher = await dbContext.Teachers.FirstOrDefaultAsync(t => t.Id == teacherUpdateRequest.Id);
        if (teacher == null)
        {
            return false;
        }

        teacher.TcrMajor = teacherUpdateRequest.TcrMajor;
        teacher.TcrQual = teacherUpdateRequest.TcrQual;
        teacher.TcrEduBg = teacherUpdateRequest.TcrEduBg;
        teacher.TcrWorkExp = teacherUpdateRequest.TcrWorkExp;
        teacher.TcrWorkAddr = teacherUpdateRequest.TcrWorkAddr;
        teacher.TcrEmpDate = teacherUpdateRequest.TcrEmpDate;
        teacher.ColleId = teacherUpdateRequest.ColleId;
        teacher.DepId = teacherUpdateRequest.DepId;
        teacher.PsnId = teacherUpdateRequest.PsnId;
        teacher.Remark = teacherUpdateRequest.Remark;

        await dbContext.SaveChangesAsync();
        return true;
    }

    public async Task<PagedResult<TeacherResult>> GetTeacherPagedAsync(PagedRequest request)
    {
        var query = from teacher in dbContext.Teachers
            join person in dbContext.People on teacher.PsnId equals person.Id
            join user in dbContext.Users on person.Id equals user.PsnId
            join college in dbContext.Colleges on teacher.ColleId equals college.Id
            join department in dbContext.Departments on teacher.DepId equals department.Id into DepartmentGroup
            from department in DepartmentGroup.DefaultIfEmpty()
            select new TeacherResult
            {
                Id = teacher.Id,
                TcrMajor = teacher.TcrMajor,
                TcrQual = teacher.TcrQual,
                TcrEduBg = teacher.TcrEduBg,
                TcrWorkExp = teacher.TcrWorkExp,
                TcrWorkAddr = teacher.TcrWorkAddr,
                TcrEmpDate = teacher.TcrEmpDate,
                ColleId = teacher.ColleId,
                DepId = teacher.DepId,
                PsnId = teacher.PsnId,
                Remark = teacher.Remark,
                CreatedBy = teacher.CreatedBy,
                CreatedTime = teacher.CreatedTime,
                UpdatedBy = teacher.UpdatedBy,
                UpdatedTime = teacher.UpdatedTime,
                PsnName = person.PsnName,
                PsnGen = person.PsnGen,
                PsnIdType = person.PsnIdType,
                PsnIdNum = person.PsnIdNum,
                PsnNat = person.PsnNat,
                PsnRen = person.PsnRen,
                PsnAddr = person.PsnAddr,
                PsnBirDate = person.PsnBirDate,
                PsnBirAddr = person.PsnBirAddr,
                UserId = user.Id,
                UserEmail = user.UserEmail,
                UserPhone = user.UserPhone,
                UserAccount = user.UserAccount,
                UserPassword = user.UserPassword,
                UserRole = user.UserRole,
                ColleName = college.ColleName,
                DepName = department.DepName
            };

        return await GetPagedQueryResultAsync(query, request);
    }
}